package com.lyhengtongwl.zqsnews.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Steven on 2019/1/16.
 */
public class AppraiseAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
    private Context mContext;
    private List<Object> list;

    public AppraiseAdapter(Context mContext, @Nullable List<Object> data) {
        super(R.layout.appraise_item, data);
        this.list = data;
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        JSONObject object = ((JSONObject) (JSON.toJSON(list.get(helper.getPosition()))));
        Picasso.with(App.getContext()).load(object.getString("userAvatar")).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.people_img));
        String imageUrl;
        if (object.containsKey("imageUrl")) {
            imageUrl = object.getString("imageUrl");
            if (!TextUtils.isEmpty(imageUrl)) {
                String lastChar = imageUrl.substring(imageUrl.length() - 1);
                if (",".equals(lastChar)) {
                    imageUrl = imageUrl.substring(0, imageUrl.length() - 1);
                }
                String[] url = imageUrl.split(",");
                if (url.length > 0) {
                    helper.getView(R.id.ll_img).setVisibility(View.VISIBLE);
                    if (url.length == 1) {
                        Picasso.with(App.getContext()).load(url[0]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_1));
                    } else if (url.length == 2) {
                        Picasso.with(App.getContext()).load(url[0]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_1));
                        Picasso.with(App.getContext()).load(url[1]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_2));
                    } else {
                        Picasso.with(App.getContext()).load(url[0]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_1));
                        Picasso.with(App.getContext()).load(url[1]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_2));
                        Picasso.with(App.getContext()).load(url[2]).resize(dip2px(250), dip2px(250)).into((ImageView) helper.getView(R.id.goods_3));
                    }
                } else {
                    helper.getView(R.id.ll_img).setVisibility(View.GONE);
                }
            } else {
                helper.getView(R.id.ll_img).setVisibility(View.GONE);
            }
        } else {
            helper.getView(R.id.ll_img).setVisibility(View.GONE);
        }

//        people_img
        helper.setText(R.id.account, object.getString("userAccount"));
        helper.setText(R.id.time, timeStamp2Date(object.getJSONObject("addTime").getString("time")));
        helper.setText(R.id.detail, object.getString("content"));
    }


    private static String timeStamp2Date(String seconds) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        long now = Long.parseLong(seconds);      //获取当前时间，毫秒形式
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);
        return format.format(calendar.getTime());
    }

    private int dip2px(float dpValue) {
        float scale = mContext.getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
